import {defineStore} from 'pinia'
import {reactive, ref} from 'vue'
import {defaultCountList, defaultLoading} from '@/store/home/config'
import {cloudGeneralObject} from '@/uniCloud-alipay'

const useHomeStore = defineStore('home', () => {
  //统计条数
  const countList = reactive(defaultCountList)
  //加载状态
  const isLoading = ref(defaultLoading)

  //请求总数
  const fetchCountListAction = async () => {
    isLoading.value = true

    //逐个请求数据库
    for (const key in defaultCountList) {
      countList[key].count = (await cloudGeneralObject.getCount(key)).data.total
    }

    //设置加载状态
    isLoading.value = false
  }

  return {countList, isLoading, fetchCountListAction}
})

export default useHomeStore
